﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System;


[System.Serializable]
public class GameConfigData
{
    //标志
    public string session;
    //服务地址
    public string server;
    //游戏名
    public string gamename = "gamename";
    //游戏代码
    public string gamecode = "code";
    //版本号
    public string vesion = "1.0.0";
    //游戏难度等级
    public int level = 1;
    //每次需要投多少币
    public int gameneedcoin = 3;
    //记录时间 网络提交数
    public string reocedtime;
    //是否启动彩券
    public int ticket = 0;
    //多少积分兑一彩券
    public int scorechangticket = 1000;
    //累计多少积分发一卡;
    public int scorechangCard = 10000;
    //累计发卡分值 
    public int overCardScore = 0;


    //今天
    public double coins_today = 0;

    //7天
    public double coins_7 = 0;
    //30天
    public double coins_30 = 0;
    //360天
    public double coins_365 = 0;
    //总投币数
    public double coins_all = 0;
    //记录币数
    public double reoced_coins = 0;


    //今天
    public double tickets_today = 0;

    //7天
    public double tickets_7 = 0;
    //30天
    public double tickets_30 = 0;

    //360天
    public double tickets_365 = 0;
    //总投币数
    public double tickes_all = 0;

    //记录点券数
    public double reoced_tickes = 0;


    //今天
    public double cards_today = 0;
    //7天
    public double cards_7 = 0;
    //30天
    public double cards_30 = 0;

    //360天
    public double cards_365 = 0;
    //总投发卡
    public double cards_all = 0;

    //记录发卡
    public double reoced_cards = 0;


    //游戏次数据
    public int run_game = 0;

    public string savetime;



    private static GameConfigData _configInstant = null;
    public static GameConfigData instantiation()
    {
        if (_configInstant == null)
            _configInstant = GameConfigData.LoadFromFile();

        return _configInstant;
    }
    public void rest()
    {
        tickes_all = 0;
        tickets_365 = 0;
        tickets_30 = 0;
        tickets_7 = 0;
        tickets_today = 0;

        coins_all = 0;
        coins_365 = 0;
        coins_30 = 0;
        coins_7 = 0;
        coins_today = 0;

        scorechangticket = 1000;
        ticket = 1;
        gameneedcoin = 3;
        level = 1;
    }

    public bool addOverCardScore(int a)
    {
        overCardScore += a;
        if (overCardScore > scorechangCard)
        {
            overCardScore = 0;
            return true;
        }
        return false;
    }
    public void addSendCardNumber(int a)
    {
        scorechangCard += a;
        if (scorechangCard < 100000)
            scorechangCard = 100000;
    }
    public void addCointnumber(int i)
    {
        gameneedcoin += i;
        if (gameneedcoin < 1)
            gameneedcoin = 1;
    }
    public void addtikernumber(int i)
    {
        scorechangticket += i;
        if (scorechangticket < 1000)
            scorechangticket = 1000;
    }

    //发卡数量
    public void AddCards(int i = 1)
    {
        cards_all += i;

        DateTime saveT = getSaveTime();
        DateTime nT = DateTime.Now;
        if (nT.Year != saveT.Year)
            cards_365 = 0;
        if (nT.Month != saveT.Month)
            cards_30 = 0;

        if (nT.DayOfYear != saveT.DayOfYear)
            cards_today = 0;

        DateTime startweek = nT.AddDays(1 - Convert.ToInt32(nT.DayOfWeek.ToString("d")));
        DateTime startsweek = saveT.AddDays(1 - Convert.ToInt32(saveT.DayOfWeek.ToString("d")));
        if (startweek.Day != startsweek.Day)
            cards_7 = 0;


        cards_365 += i;
        cards_30 += i;
        cards_today += i;
        cards_7 += i;
    }

    //添金币
    public void AddCoin(int i = 1)
    {
        coins_all += i;

        DateTime saveT = getSaveTime();
        DateTime nT = DateTime.Now;
        if (nT.Year != saveT.Year)
            coins_365 = 0;
        if (nT.Month != saveT.Month)
            coins_30 = 0;

        if (nT.DayOfYear != saveT.DayOfYear)
            coins_today = 0;

        DateTime startweek = nT.AddDays(1 - Convert.ToInt32(nT.DayOfWeek.ToString("d")));
        DateTime startsweek = saveT.AddDays(1 - Convert.ToInt32(saveT.DayOfWeek.ToString("d")));
        if (startweek.Day != startsweek.Day)
            coins_7 = 0;


        coins_365 += i;
        coins_30 += i;
        coins_today += i;
        coins_7 += i;

    }

    //彩券
    public void AddTicke(int i = 1)
    {
        tickes_all += i;

        DateTime saveT = getSaveTime();
        DateTime nT = DateTime.Now;

        if (nT.Year != saveT.Year)
            tickets_365 = 0;
        if (nT.Month != saveT.Month)
            tickets_30 = 0;

        if (nT.DayOfYear != saveT.DayOfYear)
            tickets_today = 0;

        DateTime startweek = nT.AddDays(1 - Convert.ToInt32(nT.DayOfWeek.ToString("d")));
        DateTime startsweek = saveT.AddDays(1 - Convert.ToInt32(saveT.DayOfWeek.ToString("d")));
        if (startweek.Day != startsweek.Day)
            tickets_7 = 0;

        tickets_365 += i;
        tickets_30 += i;
        tickets_today += i;
        tickets_7 += i;
    }

    public DateTime getRecodeTime()
    {
        if (reocedtime == null)
            return DateTime.Now;

        DateTime ds = Convert.ToDateTime(reocedtime);
        return ds;
    }

    public DateTime getSaveTime()
    {

        if (savetime == null)
            return DateTime.Now;

        DateTime ds = Convert.ToDateTime(savetime);
        return ds;
    }

    public void save()
    {
        GameConfigData.writeConfigDatas(this);
    }
    //读取配置 
    public static GameConfigData LoadFromFile()
    {
        string path = Application.streamingAssetsPath + "/KFConfig/gameconfig.json";
        Debug.Log(" read game config:" + path);
        if (!File.Exists(path))
            return new GameConfigData();

        Debug.Log("  config read: is file");

        StreamReader sr = new StreamReader(path);

        string json = sr.ReadToEnd();
        sr.Close();

        Debug.Log("  config read datas: " + json);

        if (json != null && json.Length > 0)
        {
            GameConfigData datas = JsonUtility.FromJson<GameConfigData>(json);
            if (datas != null)
                return datas;
        }

        return new GameConfigData();
    }

    //写入配置文件
    public static void writeConfigDatas(GameConfigData data)
    {
        data.savetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        string path = Application.streamingAssetsPath + "/KFConfig/gameconfig.json";
        string json = JsonUtility.ToJson(data, true);
        Debug.Log("save config url:" + path);
        Debug.Log("save:" + json);
        File.WriteAllText(path, json, Encoding.UTF8);
    }
}
